<!-- 退货原因 -->
<template>
  <div class="returnGoodsReason sys-content">
    <page-mod>
      <el-card>
        <div class="query-box">
          <el-form :model="query" inline>
            <el-form-item label="退货原因:">
              <el-input v-model="query.reasonName" placeholder="请输入退货原因" clearable/>
            </el-form-item>
          </el-form>
          <div class="btns">
            <el-button icon="Search" color="#16b777" @click="load(1)">查询</el-button>
            <el-button icon="Plus" color="#FFB800" @click="show = true">新增</el-button>
            <el-button icon="Refresh" color="#FF5722" @click="resetSearch">重置</el-button>
          </div>
        </div>
      </el-card>
    </page-mod>
    <page-mod title="退货原因数据" icon="Memo" :showBtn="false" :padBottom="false">
      <el-card v-loading="loading" element-loading-text="拼命加载中" >
        <el-table :data="dataList" max-height="493" row-key="id" border>
          <el-table-column label="序号" type="index" align="center" width="60"></el-table-column>
          <el-table-column label="退货原因" prop="reasonName" align="center"></el-table-column>
          <el-table-column label="排序" prop="orderNum" align="center"></el-table-column>
          <el-table-column label="是否启用" prop="enableStatus" align="center">
            <template #default="{row}">
              <el-switch 
                v-model="row.enableStatus" 
                :active-value="1" 
                :inactive-value="0" 
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="enableChange(row)"/>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="200">
            <template #default="{row}">
              <el-button type="primary" icon="Edit" link @click="toUpdate(row)">修改</el-button>
              <el-divider direction="vertical" />
              <el-button type="danger" icon="Delete" link @click="doDelete(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination 
          class="paging"
          v-show="query.total"
          small
          layout="total, sizes, prev, pager, next, jumper" 
          :current-page="query.current" 
          :page-size="query.size"
          :total="query.total"
          @current-change="load"
          @size-change="sizeChange">
        </el-pagination>
      </el-card>
    </page-mod>

    <!-- 新增、编辑弹窗 -->
    <el-dialog v-model="show" :title="form.id ? '编辑' : '新增'" top="10vh" width="23%" center draggable @close="resetForm">
      <el-form ref="formRef" :model="form" :rules="rules" size="default" label-width="85px">
        <el-form-item label="退货原因:" prop="reasonName" required>
          <el-input v-model="form.reasonName" placeholder="请输入退货原因" clearable/>
        </el-form-item>
        <el-form-item label="排序:" prop="orderNum">
          <el-input-number v-model="form.orderNum" :min="1"></el-input-number>
        </el-form-item>
        <el-form-item label="状态:" prop="enableStatus">
          <el-radio-group v-model="form.enableStatus">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" size="default" @click="doSubmit">保存</el-button>
        <el-button size="default" @click="show = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="returnGoodsReason">
  import { cudReturnGoodsReasonApi, getReturnGoodsReasonListPageApi, updateReturnGoodsReasonStatusApi } from '@/api/api'
  import { ref, reactive, toRefs, getCurrentInstance, onActivated } from 'vue'

  // 获取注册的全局实列
  const currentThis = getCurrentInstance().appContext.config.globalProperties
  const formRef = ref(null)

  const currentData = reactive({
    loading: false,
    show: false,
    query: {
      reasonName: '',
      current: 1,
      size: 20,
      total: 0
    },
    dataList: [],
    form: {
      id: '',
      reasonName: '',
      orderNum: 1,
      enableStatus: 1
    },
    rules: {
      reasonName: [{ required: true, message: '请输入退货原因', trigger: ['blur', 'change'] }],
    }
  })
  let {loading, show, query, dataList, form, rules} = toRefs(currentData)

  // 查询
  function load(page) {
    currentData.loading = true
    currentData.query.current = page
    getReturnGoodsReasonListPageApi(currentData.query).then(res => {
      currentData.loading = false
      if(res.code === '200') {
        currentData.dataList = res.data.pages
        currentData.query.total = res.data.total
      } else {
        currentThis.$message.error(res.message)
      }
    })
  }

  // 新增或编辑
  function doSubmit() {
    formRef.value.validate((valid) => {
      if (valid) {
        currentThis.$messageBox.confirm(`确认${currentData.form.id ? '修改' : '新增'}吗？`).then(() => {
          let loading = currentThis.$loading(`正在${currentData.form.id ? '修改' : '新增'}`)
          cudReturnGoodsReasonApi(`${currentData.form.id ? 'update' : 'save'}`, currentData.form).then(res => { // 修改
            loading.close()
            if(res.code === '200') {
              currentData.show = false
              load(currentData.form.id ? currentData.query.current : 1)
              currentThis.$message.success(res.message)
            } else {
              currentThis.$message.error(res.message)
            }
          })
        }).catch(() => {})
      } else {
        return false;
      }
    })
  }

  // 修改前弹窗
  function toUpdate(e) {
    currentData.form = {...e}
    currentData.show = true
  } 

  // 删除
  function doDelete(id) {
    currentThis.$messageBox.confirm('确认删除吗？').then(() => {
      let loading = currentThis.$loading('删除中，请耐心等待')
      cudReturnGoodsReasonApi('delete', { id: id }).then(res => {
        loading.close()
        if(res.code === '200') {
          load(currentData.query.current)
          currentThis.$message.success(res.message)
        } else {
          currentThis.$message.error(res.message)
        }
      })
    }).catch(() => {})
  }

  // 修改启用状态
  function enableChange(e) {
    updateReturnGoodsReasonStatusApi(e.id, e.enableStatus).then(res => {
      if(res.code === '200') {
        load(currentData.query.current)
        currentThis.$message.success(res.message)
      } else {
        currentThis.$message.error(res.message)
      }
    })
  }

  // 重置查询
  function resetSearch() {
    currentData.query.reasonName = ''
    load(1)
  }
  
  // 新增编辑弹窗重置数据
  function resetForm() {
    currentData.form = {
      id: '',
      reasonName: '',
      orderNum: 1,
      enableStatus: 1
    }
    formRef.value.resetFields()
  }

  function sizeChange(size) {
    currentData.query.size = size
    load(1)
  }

  onActivated(() => {
    load(currentData.query.current)
  })
</script>

<style lang="less" scoped>
</style>